﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Win32;
using RHLogReader.Classes;
using RHLogReader.Model;
using System.ComponentModel;
using System.IO;

namespace RHLogReader.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        #region Variables
        private readonly IDataService _dataService;

        private FileInfo _SelectedFile;
        public FileInfo SelectedFile
        {
            get { return _SelectedFile; }
            set
            {
                _SelectedFile = value;
                this.RaisePropertyChanged(() => this.SelectedFile);
            }
        }

        private string _ChatOutput;
        public string ChatOutput
        {
            get { return _ChatOutput; }
            set
            {
                _ChatOutput = value;
                this.RaisePropertyChanged(() => this.ChatOutput);
            }
        }

        private MessageBox _MainBox;
        public MessageBox MainBox
        {
            get { return _MainBox; }
            set
            {
                _MainBox = value;
                this.RaisePropertyChanged(() => this.MainBox);
            }
        }
        #endregion

        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;

            CommandsInitializer();
            BackgroundWorkersInitializer();
        }

        #region Commands
        public RelayCommand BrowseFileCommand { get; set; }
        public RelayCommand<System.Windows.Controls.DataGridRowEditEndingEventArgs> ChannelsEditEndingCommand { get; set; }

        void CommandsInitializer()
        {
            BrowseFileCommand = new RelayCommand(OnBrowseFileCommand);
            ChannelsEditEndingCommand = new RelayCommand<System.Windows.Controls.DataGridRowEditEndingEventArgs>(OnChannelsEditEndingCommand);
        }

        private void OnChannelsEditEndingCommand(System.Windows.Controls.DataGridRowEditEndingEventArgs obj)
        {
            Channel editetChanell = obj.Row.Item as Channel;
        }

        private void OnBrowseFileCommand()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.DefaultExt = ".txt";
            fileDialog.Filter = "Text documents (.txt)|*.txt";

            if (fileDialog.ShowDialog() == true)
            {
                SelectedFile = new FileInfo(fileDialog.FileName);
            }

            _BWLoadFileContent.RunWorkerAsync();
        }
        #endregion

        #region Background Workers
        private BackgroundWorker _BWLoadFileContent { get; set; }

        void BackgroundWorkersInitializer()
        {
            _BWLoadFileContent = new BackgroundWorker();
            _BWLoadFileContent.WorkerReportsProgress = true;
            _BWLoadFileContent.RunWorkerCompleted += _BWLoadFileContent_RunWorkerCompleted;
            _BWLoadFileContent.DoWork += _BWLoadFileContent_DoWork;
            _BWLoadFileContent.ProgressChanged += _BWLoadFileContent_ProgressChanged;
        }

        void _BWLoadFileContent_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            throw new System.NotImplementedException();
        }

        void _BWLoadFileContent_DoWork(object sender, DoWorkEventArgs e)
        {
            if (SelectedFile != null)
            {
                using (StreamReader stream = new StreamReader(SelectedFile.FullName))
                {
                    e.Result = stream.ReadToEnd();
                }
            }
        }

        void _BWLoadFileContent_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ChatOutput = "";

            LogParser parser = new LogParser(e.Result as string);
            MessageBox logBox = parser.Parse();

            if (MainBox == null)
                MainBox = logBox;

            ChatOutput = MainBox.ToString();
        }

        #endregion

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}